﻿#ifndef YUTOPP_HATENA_REQUEST_API_DIARY_HPP
#define YUTOPP_HATENA_REQUEST_API_DIARY_HPP

#include "../../../url_base64.hpp"

#include "../../basic_api.hpp"
#include "../group.hpp"

namespace webapp
{
	namespace clients
	{
		namespace hatena
		{
			namespace api
			{
				namespace detail_
				{
					struct body_process
					{
						void operator()( API_GET_PARAMETER_TYPE(atom, std::string)& t ) const
						{
							t.erase( "holder" );
						}
						void operator()( API_GET_PARAMETER_TYPE(atom, std::wstring)& t ) const
						{
							t.erase( L"holder" );
						}
					};

					struct address_process_username
					{
						std::string operator()(
							const char* t,
							const API_GET_PARAMETER_TYPE(atom, std::string)& p ) const
						{
							boost::format f( t );
							f % p.get<std::string>( "holder.__username" );
							return f.str();
						}
						std::string operator()(
							const char* t,
							const API_GET_PARAMETER_TYPE(atom, std::wstring)& p ) const
						{
							boost::format f( t );
							f % url::encode( p.get<std::wstring>( L"holder.__username" ) );
							return f.str();
						}
					};
				}

				namespace diary
				{
					//atom[GET]
					API_ENTRY_START( atom, "%1%/atom", get, atom )
						API_GROUP( group_diary )
						API_PARAMETER_LIST_OWNER(
							API_TEMPLATE_TYPE_STRING()
							API_PARAMETER_STRING( username, , "__username" )
						)
						{
							API_MAKE_RETUTN_VAL( std::string, tree );
							tree.put( "holder.__username", username );

							return tree;
						}

						typedef detail_::body_process				meta_body_process;
						typedef detail_::address_process_username	meta_address_process;
					API_ENTRY_END


					//post[GET]
					API_ENTRY_START( post, "%1%/atom/blog", post, atom )
						API_GROUP( group_diary )
						API_PARAMETER_LIST_OWNER(
							API_TEMPLATE_TYPE_STRING()
							API_PARAMETER_STRING( username, , /*"__username"*/ )
							API_PARAMETER_STRING( title, , )
							API_PARAMETER_STRING( content, , )
							API_PARAMETER_STRING( type, "text/plain", )
							API_PARAMETER_STRING( updated, "", )
						)
						{
							API_MAKE_RETUTN_VAL( std::string, tree );
							tree.put( "holder.__username", username );

							tree.add( "entry", "" );
							tree.add( "entry.<xmlattr>.xmlns", "http://purl.org/atom/ns#" );

							tree.add( "entry.title", title );
							tree.add( "entry.content", content );
							tree.add( "entry.content.<xmlattr>.type", type );
							
							tree.add( "entry.updated", updated );
							
							return tree;
						}
						API_PARAMETER_LIST_OWNER(
							API_TEMPLATE_TYPE_WSTRING()
							API_PARAMETER_WSTRING( username, , /*"__username"*/ )
							API_PARAMETER_WSTRING( title, , )
							API_PARAMETER_WSTRING( content, , )
							API_PARAMETER_WSTRING( type, L"text/plain", )
							API_PARAMETER_WSTRING( updated, L"", )
						)
						{
							API_MAKE_RETUTN_VAL( std::wstring, tree );
							tree.put( L"holder.__username", username );

							tree.add( L"entry", "" );
							tree.add( L"entry.<xmlattr>.xmlns", "http://purl.org/atom/ns#" );

							tree.add( L"entry.title", title );
							tree.add( L"entry.content", content );
							tree.add( L"entry.content.<xmlattr>.type", type );
							
							tree.add( L"entry.updated", updated );
							
							return tree;
						}

						typedef detail_::body_process				meta_body_process;
						typedef detail_::address_process_username	meta_address_process;
					API_ENTRY_END

				} // - dialy


/*				using namespace ::webapp::clients::api;

				//
				namespace bookmark
				{
					//statuses/home_timeline[GET]
					struct endpoint
						: public atom_data, tag_get_method
					{
						param_type operator()(
							  s since_id = ""
							, s trim_user = ""
							, s include_entities = ""
							) const
						{
							return boost::assign::map_list_of
									( "since_id",			since_id )
									( "trim_user",			trim_user )
									( "include_entities",	include_entities );
						}
					};
				}*/


			} // - api
		} // - hatena
	} // - clients
} // - webapp

#endif /*YUTOPP_HATENA_REQUEST_API_DIARY_HPP*/
